/****************************************************************************************************************

Updated [Arun Ramachandran[2/17]] - Updated the code coverage with the test class

*****************************************************************************************************************/

public class OOR_CreateOppOnRecurringChange{
    
     public static void CreateOrder(List<Account> AccList){
         List<Opportunity> OppList = new List<Opportunity>();
         Map<Id,User> UserMap = new Map<Id,User>();
         //User adminId=[Select Id from User where Alias='sadmin'];
         for(User singleuser:[Select id,FirstName,LastName,SFA_Office__c,Manager.FirstName,Manager.LastName,UserRole.Name from User where SFA_Office__c != NULL AND ISACTIVE = TRUE]){
             UserMap.put(singleuser.Id,singleuser);
         }
         ID SFAOORRenewalecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Renewals');
         Set<Id> OwenerSet = new Set<Id>();
         OwenerSet = UserMap.KeySet();
         for(Account acc:AccList){
             
            if(OwenerSet.Contains(acc.OwnerId)){
             Double NetRev = acc.Recurring_Charge__c - acc.OOR_Previous_Recurring_Charges__c;
             Opportunity opp = new Opportunity();
             opp.RecordTypeId = SFAOORRenewalecordTypeID;
             opp.Amount = NetRev;
             opp.Opportunity_Type__c = 'Net Revenue';
             Date Mydate = Date.Today();
             String Str = String.valueOf(Mydate);
             String StrName = Str.Substring(0,10);
             opp.Name = 'OOR Net Rev'+' '+UserMap.get(acc.OwnerId).FirstName+' '+UserMap.get(acc.OwnerId).LastName+' '+StrName;
             opp.AccountId = acc.Id;
             //opp.Primary_Phone__c = acc.Primary_Phone__c;
             ListViewIds__c adminId = ListViewIds__c.getInstance(); 
             if(adminId.SalesforceAdmin__c!=Null){
             opp.OwnerId=adminId.SalesforceAdmin__c;  
             }
             //opp.OwnerId = acc.OwnerId;
             opp.StageName = 'Completed';
             opp.CloseDate = Date.Today();
             opp.Projected_Close_Date__c = Date.Today();
             opp.Order_ID_SOLO__c = '9999999';
             opp.SOLO_Order_State__c = 'C';
             opp.Order_Completion_Date__c = opp.CloseDate;
             opp.SOLO_Submitted_Date__c = opp.CloseDate;
             opp.Order_Date_SOLO__c = opp.CloseDate;
             opp.SOLO_Contract_Date__c =  opp.CloseDate;
             opp.SOLO_Prev_Monthly_Amt__c = acc.OOR_Previous_Recurring_Charges__c;
             opp.SOLO_Contract_Revenue_mo__c = acc.Recurring_Charge__c;
             //opp.Secondary_Owner__c=acc.OOR_Secondary_Account_Owner__c;
             opp.Ext_Primary_Owner__c=acc.OwnerId;
             Opp.Ext_Secondary_Owner__c=acc.OOR_Secondary_Account_Owner__c;
             opp.Monthly_Amount__c = NetRev;
             opp.SOLO_Order_Value__c=acc.Recurring_Charge__c;
             opp.SOLO_Prev_Order_Value__c=acc.OOR_Previous_Recurring_Charges__c;
             opp.Revenue_mo__c = NetRev;
             opp.SOLO_Manager_P_Owner__c = UserMap.get(acc.OwnerId).Manager.FirstName+' '+UserMap.get(acc.OwnerId).Manager.LastName;
             opp.SOLO_Office_P_Owner_Code__c = UserMap.get(acc.OwnerId).SFA_Office__c;
             if(acc.OOR_Secondary_Account_Owner__c!=NULL){
             opp.SOLO_Office_S_Owner_Code__c=UserMap.get(acc.OOR_Secondary_Account_Owner__c).SFA_Office__c;
             opp.SOLO_Manager_S_Owner__c=UserMap.get(acc.OOR_Secondary_Account_Owner__c).Manager.FirstName+' '+UserMap.get(acc.OOR_Secondary_Account_Owner__c).Manager.LastName;
             }
             OppList.add(opp);
            } 
         }
         if(OppList.size()>0){
            try{
            Insert OppList;
            }
            Catch(DMLException e){
                system.debug('DMLException'+e);
            }
        }
     }
     
     //Added test class by Arun Ramachandran on 2/17 for fixing coverage issues.
     
     static testMethod void testOOR_CreateOppOnRecurringChange(){
         Profile p = [select id from profile where name='SFA OOR Media Consultants'];
         User u2 = new User(alias = 'Opt2', email='sysadminOpty2@accenture.com',emailencodingkey='UTF-8', lastname='Test Opty 2', languagelocalekey='en_US',localesidkey='en_US', profileid = p.Id,timezonesidkey='Asia/Calcutta', username='testOppty2@testorg.com',SFA_Office__c='LVN',IsActive = True);        
         Insert u2;
         ID SFAOORAccountecordTypeID = RecordTypeHelper.GetRecordTypeID('Account', 'Local OOR Customer');
         Account Acct1 = new Account();
         Acct1.Name = 'TestAccount121';
         Acct1.Phone = '9990009991';
         Acct1.RecordtypeId = SFAOORAccountecordTypeID;
         Acct1.Recurring_Charge__c = 3000;
         Acct1.OwnerID = u2.id;
         Insert Acct1;
         
         Acct1.Recurring_Charge__c = 2000;
         Update Acct1;
     
     }
 }